<template>
  <!-- 底部导航栏 -->
  <footer class="bottom-nav">
    <div 
      v-for="(item, index) in navItems" 
      :key="index"
      :class="['nav-item', { active: activeIndex === index, 'add-btn': item.isAddBtn }]"
      @click="handleNavClick(index, item.path)"
    >
      <Icon :name="item.icon" />
      <span v-if="!item.isAddBtn">{{ item.title }}</span>
    </div>
  </footer>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Icon } from 'vant';

const route = useRoute();
const router = useRouter();

// 导航项数据
const navItems = ref([
  {
    title: '首页',
    icon: 'home-o',
    path: '/home'
  },
  {
    title: '论坛', 
    icon: 'comment-o',
    path: '/forum'
  },
  {
    title: '发布',
    icon: 'plus',
    path: '/publish',
    isAddBtn: true
  },
  {
    title: '商城',
    icon: 'shop-o', 
    path: '/store'
  },
  {
    title: '我的',
    icon: 'user-o',
    path: '/profile'
  }
]);

const activeIndex = ref(0);

// 根据当前路由自动激活对应的导航项
watch(() => route.path, (newPath) => {
  const index = navItems.value.findIndex(item => item.path === newPath);
  if (index !== -1) {
    activeIndex.value = index;
  }
}, { immediate: true });

// 处理导航点击
const handleNavClick = (index, path) => {
  activeIndex.value = index;
  router.push(path);
};

</script>

<style lang="less" scoped>
.bottom-nav {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 8px 0 12px;
  background-color: white;
  box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.1);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: 60px;
  box-sizing: border-box;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #666;
  cursor: pointer;
  width: 50px;
  transition: color 0.2s ease;

  &:hover {
    color: #ff2442;
  }

  &.active {
    color: #ff2442;
  }

  .van-icon {
    font-size: 22px;
    margin-bottom: 2px;
  }

  span {
    font-size: 11px;
    font-weight: 500;
  }
}

/* 中间的加号按钮 */
.add-btn {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: #ff2442;
  color: white !important;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: -20px;
  box-shadow: 0 2px 8px rgba(255, 36, 66, 0.3);
  transition: transform 0.2s ease, box-shadow 0.2s ease;

  &:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(255, 36, 66, 0.4);
  }

  .van-icon {
    font-size: 26px;
    margin-bottom: 0;
  }
}

/* 响应式调整 */
@media screen and (max-width: 375px) {
  .nav-item {
    width: 45px;
    
    .van-icon {
      font-size: 20px;
    }
    
    span {
      font-size: 10px;
    }
  }
  
  .add-btn {
    width: 45px;
    height: 45px;
    
    .van-icon {
      font-size: 24px;
    }
  }
}
</style>